/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package application.dao;

import application.model.EntityAccomplishment;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
//import org.springframework.jdbc.core.JdbcTemplate;


/**
 *
 * @author aa6345
 */
@Repository("entityAccomplishmentDao")
public class EntityAccomplishmentDaoImpl implements EntityAccomplishmentDao {

    @Autowired
    private SessionFactory sessionFactory;

    /*
    // Try to use Spring JDBC intead of Hibernate
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

     */

    // To get list of all 
    @SuppressWarnings("unchecked")
    public List<EntityAccomplishment> listSelectedEntityAccomp(String entityID) {

        //TODO: For spring JDBC look at http://static.springsource.org/spring/docs/3.0.x/reference/jdbc.html
        //TODO: Somehow get Connection since sessionFactory.getCurrentSession().connection() is deprecated
        //TODO: Need this to use PreparedStatement which gards against SQL Injection
        /*                
        PreparedStatement prepStmt = sessionFactory.getCurrentSession().connection().prepareStatement(selectStatement);
        prepStmt.setString(1, userInputHasMap);
        ResultSet rs = prepStmt.executeQuery();
         */

        List<EntityAccomplishment> selectedEntityAccomps = new ArrayList<EntityAccomplishment>();        

        if (this.validateStringInput(entityID) == true) {

            String searchQuery = "select `ea`.`entityID` as entityID,"
                    + "`ea`.`accompTypeID` as accompTypeID, "
                    + "`ea`.`title` as title,  "
                    + "`ea`.`description` as description,  "
                    + "`ea`.`priority` as priority,  "
                    + "`ea`.`entityAccomplishmentID` as entityAccomplishmentID,  "
                    + "`c`.`email` as email,  "                    
                    + "`at`.`accompTypeDescription` as accompTypeDescription "
                    + "from entityaccomplishment ea "
                    + "LEFT JOIN contactdetails c on ea.entityID=c.entityID "
                    + "LEFT JOIN accomplishmenttype at on ea.accompTypeID=at.accompTypeID "
                    + "WHERE ea.entityID=" + entityID + " ";

            System.out.println("*****THE QUERY WOULD BE: " + searchQuery);

            selectedEntityAccomps = sessionFactory.getCurrentSession().createSQLQuery(searchQuery).addEntity(EntityAccomplishment.class).list();

            
        }

        return selectedEntityAccomps;
       
    }

    private Boolean validateStringInput(String inputString) {
        Boolean isValid = false;

        try {
            int inputNumber = Integer.parseInt(inputString.trim());
            if(inputNumber > 0) {
                isValid = true;
            }
        } catch (NumberFormatException nfe) {
            System.out.println("Error converting '"+inputString+"' to Integer: "+nfe);
            isValid = false;
        }

        return isValid;
    }
}
